package com.open4cn.simulator.connect.tcp.listenner;

import com.open4cn.simulator.core.connector.ConnectStatus;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.util.concurrent.GenericFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description 通道连接状态监听器
 * @Email yinxiaopeng@zj.tech
 * @date 2022-04-05 18:12
 */
@ChannelHandler.Sharable
public class ConnectStatusFutureListener implements GenericFutureListener<ChannelFuture> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private ConnectStatus connectStatus;

    public ConnectStatusFutureListener(ConnectStatus connectStatus) {
        this.connectStatus = connectStatus;
    }

    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
        logger.info("current channel status: {}", future.channel());
        boolean success = future.isSuccess();
        if (success) {
            connectStatus.beConnect();
        } else {
            future.channel().pipeline().fireChannelInactive();
            connectStatus.beFail();
        }
    }
}
